package wiki.servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import wiki.generic.utilities.PageNotFoundException;
import wiki.setup.Setup;
/**
 * 
 * @author <p><strong>JwTeam</strong></p> 
 * Responsabile della gestione di video (you tube)
 */
public class YoutubeModServlet extends HttpServlet{

	
	/**
	 * 
	 */
	private static final long serialVersionUID = -7816973008688529932L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		
		HttpSession ses=req.getSession(false);
		if(ses!=null){
			boolean isDoc = (Boolean) ses.getAttribute("rights");
			if(isDoc){
		String name=req.getParameter("name");
		String type=req.getParameter("type");
		
		PrintWriter out=resp.getWriter();
		out.write("<html>\n<head>" +
				"<title>Login</title>\n" +
				"<link rel=\"stylesheet\" type=\"text/css\" href=\"./html/LOGIN/login2.css\"/>\n" +
				"</head>\n" +
				"<body>\n" +
				"<div id=\"contents\">\n" +
						"<form action=\"./ymod?name="+name+"&type="+type+"\" method=\"post\">\n" +
						"<h3>Modifica Link a Youtube:</h3>\n" +
						"<table>\n" +
						"<a href='http://www.youtube.com'><img src=\"html/immagini/youtube3.png\" /></a>" +
						"<tr>\n" +
						"<td class=\"col1\">Url*:</td>" +
						"<td><input type=\"text\" name=\"URL\" value=\"\" /></td>" +
						"</tr>\n" +
						"</table>\n" +
						"<input type=\"submit\" value=\"Modifica\"  id=\"submit\" name=\"submit\" />" +
						"</form>\n" +
						"</div>\n" +
						"<div id=\"footer\">Powered by JwTeam</div>\n" +
						"</body>\n</html>");
			} else{
				resp.sendRedirect("./html/errorpages/nopermessi.html");
				return;
			}
		} else{
			resp.sendRedirect("./html/LOGIN/login.html");
		}
	}
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		HttpSession ses=req.getSession(false);
		if(ses!=null){
			boolean isDoc = (Boolean) ses.getAttribute("rights");
			if(isDoc){
		String corso=(String) ses.getAttribute("corso");
		String current=(String) ses.getAttribute("current");
		String name=req.getParameter("name");
		String type=req.getParameter("type");
		String URL=req.getParameter("URL");
		URL=URL.replace("$$||$$", "$$$$$$");
		
		try {
			int id=Setup.getSetup().getMediator().geteService().insertElement(corso,type, URL);
			Setup.getSetup().getMediator().geteService().modifyContent(corso, current, name, id, new ArrayList<String>());
			resp.sendRedirect("./view?page="+current);
		} catch (PageNotFoundException e) {
			resp.sendRedirect("./html/errorpages/pagina404.html");
		}
			} else{
				resp.sendRedirect("./html/errorpages/nopermessi.html");
				return;
			}
		} else{
			resp.sendRedirect("./html/LOGIN/login.html");
			return;
		}
	}
	
	
}
